package com.financing.platform.web.api;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.AccessInfoDTO;
import com.financing.platform.dto.OrderTmpDTO;
import com.financing.platform.dto.UserInfoDTO;
import com.financing.platform.service.IFaUserService;
import com.financing.platform.service.IOrderPhoService;
import com.financing.platform.service.IOrderTmpService;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.util.DateUtil;
import com.financing.platform.vo.OrderPhoVO;
import com.financing.platform.vo.OrderVO;

@Controller("orderTmpActionApi")
@RequestMapping("/api/order")
@Scope("prototype")
public class OrderTmpAction  extends BaseAction{
	
	@Autowired
	IOrderTmpService orderTmpServiceImpl;
	
	@Autowired
	IOrderPhoService orderPhoServiceImpl;
	
	@Autowired
	IFaUserService faUserServiceImpl;
	
	@Value("#{settings['orderPreTime']}")
	private String orderPreTime;
	
	@RequestMapping("/queryOrderTmpList.shtml")
	@ResponseBody
	public ResultMap queryOrderList(HttpServletRequest request,
			HttpServletResponse response,Pager page,OrderTmpDTO orderTmpDto) {
		
		try{
			if(orderTmpDto.getSystemUserId() != null && !("").equals(orderTmpDto.getSystemUserId()) && ("isnoManagerUser").equals(orderTmpDto.getSystemUserId())){
				orderTmpDto.setSystemUserId(null);
			}
			
			// 判断当前登录者是否是订单管理员
			HttpSession session = request.getSession();
			UserInfoDTO user = (UserInfoDTO)session.getAttribute(DTOdateSet.user);
			
			if(!"admin".equals(user.getUserNickname())){
				boolean isManager = false;
				List<AccessInfoDTO> accessInfoList = user.getAccessList();
				for(AccessInfoDTO access : accessInfoList){
					if(access.getActionName().equals("addManagerOrderUser.shtml")){
						isManager = true;
						break;
					}
				}
				if(!isManager){
					orderTmpDto.setSystemUserId(user.getUserId());
				}
			}
			
			int count = orderTmpServiceImpl.queryOrderTmpCount(orderTmpDto);
			
			page.setTotalCount(count);
			
			orderTmpDto.setPageNo(page.getPageNo());
			orderTmpDto.setPageSize(page.getPageSize());
			
			List<Map<String,Object>> listOrder = orderTmpServiceImpl.queryOrderTmpList(orderTmpDto);
			
			resultMap.setAttribute("list", listOrder);
			resultMap.setAttribute("page",page);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("query order list error",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return resultMap;
	}
	
	@RequestMapping("/queryOrderTmpByProductList.shtml")
	@ResponseBody
	public ResultMap queryOrderTmpByProductList(HttpServletRequest request,
			HttpServletResponse response,Pager page,OrderTmpDTO orderTmpDto) {
		try{
			int count = orderTmpServiceImpl.countOrderTmpByProIdList(orderTmpDto);
			page.setTotalCount(count);
			orderTmpDto.setPageNo(page.getPageNo());
			orderTmpDto.setPageSize(page.getPageSize());
			List<Map<String,Object>> listOrder = orderTmpServiceImpl.queryOrderTmpByProIdList(orderTmpDto);
			
			resultMap.setAttribute("list", listOrder);
			resultMap.setAttribute("page",page);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("query order list error",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return resultMap;
	}
	
	@RequestMapping("/updateOrderTmp.shtml")
	@ResponseBody
	public ResultMap updateOrderTmp(HttpServletRequest request,
			HttpServletResponse response,OrderTmpDTO orderTmpDto) {
		
		try{
			OrderTmpDTO orderVo=new OrderTmpDTO();
			orderVo.setId(orderTmpDto.getId());
			List<OrderTmpDTO> orderTmpList = orderTmpServiceImpl.queryOrderTmpByList(orderVo);
			if(orderTmpList.size()>0){
				orderVo = orderTmpList.get(0);
			}
			if(Integer.parseInt(orderTmpDto.getStatus())<Integer.parseInt(orderVo.getStatus())){
				log.info("订单状态不能逆转!!!!");
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_ORDERBACK_ERROR);
				return resultMap;
			}
			
			// 订单如果附件没有审核成功，则不能修改确认返佣
			if("03".equals(orderTmpDto.getStatus())){
				if(DateUtil.parseDateString(orderPreTime,"yyyy-MM-dd").before(orderVo.getCreateDate())){
					OrderPhoVO orderPhoVO=new OrderPhoVO();
					orderPhoVO.setoId(orderVo.getId()+"");
					Map<String, Object> queryOrderPhoConfirm = orderPhoServiceImpl.queryOrderPhoTmpConfirm(orderPhoVO);
					int fpodIdCount=Integer.parseInt(queryOrderPhoConfirm.get("fpodId")+"");
					int fopIdCount=Integer.parseInt(queryOrderPhoConfirm.get("fopId")+"");
					if(fpodIdCount!=fopIdCount){
						log.info("还有附件未通过审核!!!!");
						resultMap.setStatus(STATUS.FAILE);
						resultMap.setStatusCode(ResultCode.STATUS_CODE_ORDERCONFIRM_ERROR);
						return resultMap;
					}
				}
				
			}
			
			resultMap.setAttribute("updateRows",orderTmpServiceImpl.updateOrderTmp(orderTmpDto));
			
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("query order list error",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return resultMap;
	}
}
